<html>
<head>
    <title>吉垚物业后台管理系统</title>
    <meta charset="utf-8">
    <link rel="shortcut icon" th:href="./images/favicon.ico" type="image/x-icon">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <!-- 引入css -->
    <link rel="stylesheet" href="./css/plugins/font-awesome-4.7.0/css/font-awesome.min.css"  media="all">
    <link rel="stylesheet" href="./lib/layui-v2.3.0/css/layui.css"  media="all">
    <link rel="stylesheet" href="./css/main.css"  media="all">
    <link rel="stylesheet" href="./css/bootstrap.min.css">
</head>
<body>
<div class="">
    <form action="#">
		  <div class="layui-form-item">
			<label class="layui-form-label">类型名</label>
			<div class="layui-input-inline">
			  <input type="text" name="typename" id="typename" lay-verify="title" autocomplete="off" placeholder="请输入类型名称" class="layui-input">
			</div>
		  </div>

		  <div >
			<label class="layui-form-label">选择框</label>
			<div class="layui-input-inline">
                <select name="interest" id="belongs" lay-filter="aihao">
			  </select>
			</div>
		  </div>
        <hr>
        <div class="layui-form-item timo-finally">
            <button class="layui-btn ajax-submit" onclick="updateType()"><i class="fa fa-check-circle"></i> 保存</button>
            <button class="layui-btn btn-secondary close-popup" id="close"><i class="fa fa-times-circle"></i> 关闭</button>
        </div>
    </form>
</div>
<script src="./lib/layui-v2.3.0/layui.js" charset="utf-8"></script>
<script src="./js/main.js" charset="utf-8"></script>
<script src="js/plugins/jquery-3.3.1.min.js"></script>
</body>
<script type="text/javascript">
    // var category=window.location.search;
    // //截取，获取获得category的具体值
    // var typeid = category.substring(category.lastIndexOf('=')+1, category.length);
    // console.log(category);
    // console.log(cateval);
    var typeid=sessionStorage.getItem('typeid');
    var name=sessionStorage.getItem('name');
    var belong=sessionStorage.getItem('belong');

    $('#typename').val(name);
        $.ajax({
            url:'/admin/getBelongs.do',
            headers:{
                accessToken:localStorage.getItem('accessToken')
            },
            success:function (data) {
                if (data.resMsg==undefined)
                    data=JSON.parse(data);
                if (data.code==401||data.code==402){
                    localStorage.clear();
                    sessionStorage.clear();
                    top.location.href='notlogin.html';
                    this.close();
                }
                console.log(data.result);
                for (var i=0;i<data.result.length;i++) {
                    console.log(data.result[i]['name']);
                    if (data.result[i]['name']==belong)
                        $('#belongs').append('<option value="'+data.result[i]['id']+'" selected>'+data.result[i]['name']+'</option')
                    else
                        $('#belongs').append('<option value="'+data.result[i]['id']+'">'+data.result[i]['name']+'</option>')
                }
            }
        })

    function updateType() {
        var name=$('#typename').val();
        var belong=$('#belongs').val();
        $.ajax({
            url:'/admin/updateType.do?name='+name+'&belong='+belong+'&id='+typeid,
            headers:{
                accessToken:localStorage.getItem('accessToken')
            },
            success:function (data) {
                if (data.resMsg==undefined)
                    data=JSON.parse(data);
                if (data.code==401||data.code==402){
                    localStorage.clear();
                    sessionStorage.clear();
                    top.location.href='notlogin.html';
                    this.close();
                }
                if (data.resMsg!='更新失败'){
                    top.location.reload();
                    this.close();
                }else{
                    alert('更新失败');
                }
            }
        })
    }

</script>
</html>